var util = require("../../../utils/util.js");
import ajax from "../../../utils/ajax.js"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: [],
    list: [],
    isTrue: false,
    name:'',
    phone:'',
    code:'',
    sexs:["男","女"],
    sex_text:'请选择性别',
    sex_id:'',
    born_date:'请选择出生年月日',
    job_id:'',
    yibaoming:false,
    get_msg_btn_is_disabled:false,
    get_msg_btn_text:'获取验证码',
    show_alert:false,
  },
  open_map(e){
    let longitude = e.currentTarget.dataset.longitude;
    let latitude = e.currentTarget.dataset.latitude;
    
    wx.openLocation({
      latitude:parseFloat(latitude),
      longitude:parseFloat(longitude),
      scale: 18
    })
  },
  get_msg_func(){
    if(!this.data.phone){
      app.toast("请输入手机号");
      return
    }
    ajax.get_msg_code("parttime_job_enroll",this.data.phone).then(res=>{
      if(res){
        let time = 60;
        this.setData({get_msg_btn_is_disabled:true})
        let timer = setInterval(() => {
          this.setData({
            get_msg_btn_text:'重新获取('+time+')'
          })
          if(time==0){
            clearInterval(timer);
            this.setData({
              get_msg_btn_text:'获取验证码',
              get_msg_btn_is_disabled:false
            })
          };
          time--
        }, 1000);
      }
    })
  },
  onLoad: function (options) {
    let id = options.id;
    this.setData({job_id:options.id})
    var param = { id: id };
    var that = this;
    util.wxRequest('parttime_jobs/detail', param, function (res) {
      let tempData = res.data.data;
      that.setData({
        detail: tempData
      })
    }, 'GET');

    this.get_more_list()
  },
  cancelZz: function () {
    this.setData({
      isTrue: false
    })
  },
  select_sex(e){
    let index = e.detail.value;
    let value = this.data.sexs[index];
    this.setData({
      sex_id:index+1,
      sex_text:value
    })
  },
  select_born_date(e){
    let value = e.detail.value;
    this.setData({
      born_date:value
    })
    console.log(value)
  },
  input_func(e) {
    let type = e.currentTarget.dataset.name;
    let val = e.detail.value;
    switch (type) {
      case "name":
        this.setData({ name: val })
        break;
      case "phone":
        this.setData({ phone: val })
        break;
        case "code":
        this.setData({ code: val })
        break;
      default:
        break;
    }
  },
  submit: function () {
    if(!this.data.name){
      app.toast("请输入真实姓名")
      return
    }
    if(!this.data.sex_id){
      app.toast("请选择性别")
      return
    }
    if(!this.data.born_date){
      app.toast("请选择出生年月日")
      return
    }
    if(!this.data.phone){
      app.toast("请输入手机号码")
      return
    }
    if(!this.data.code){
      app.toast("请输入短信验证码")
      return
    }
    let data = {
      parttime_job_id:this.data.job_id,
      realname:this.data.name,
      sex:this.data.sex_id,
      birthdate:this.data.born_date,
      mobile:this.data.phone,
      code:this.data.code
    }
    console.log(data);
    ajax.post({
      url:'parttime_jobs/enroll',
      data
    }).then(res=>{
      console.log(res);
      if(res.code==0){
        app.toast("报名成功");
        let detail = this.data.detail;
        detail.is_enroll=1
        this.setData({
          isTrue: false,
          yibaoming:true
        })
      }else{
        app.toast(res.message)
      }
    })
  },
  bm_handle(e){
    let type = e.detail.type;
    console.log(type);
    console.log(this.data.job_id)

    if(type=="confirm"){
      ajax.post({
        url:'parttime_jobs/enroll',
        data:{parttime_job_id:this.data.job_id}
      }).then(res=>{
        this.ljbm();
        console.log(res)
        if(res.code==0){
          app.toast("报名成功","succ")
        }else{
          app.toast(res.message)
        }
      })
    }
    if(type=="cancel"){
      this.ljbm()
    }
  },
  // 推荐立即报名
  tj_ljbm(e){
    let job_id = e.currentTarget.dataset.id;
    console.log(job_id)
    this.setData({
      job_id
    });
    this.ljbm();
  },
  ljbm: function () {
    let that = this;
    if(!app.common.token){
      app.toast("请先登陆");
      return;
    }
    this.setData({
      show_alert:!this.data.show_alert
    })
    return;
  },
  take_phone(e){
    console.log(e.currentTarget.dataset.phone);
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone +""
    })
  },

  // 获取更多推荐
  get_more_list(){
    ajax.get({
      url:'parttime_jobs/recommend',
      data:{
        latitude:app.common.now_position.lat,
        longitude:app.common.now_position.lng
      }
    }).then(res=>{
      console.log(res)
      this.setData({list:res.data.list_data})
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})